package com.huawei.hms.adapter.b;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.KeyEvent;
import com.huawei.hms.activity.BridgeActivity;
import com.huawei.hms.activity.IBridgeActivityDelegate;
import com.huawei.hms.adapter.internal.CommonCode.MapKey;
import com.huawei.hms.api.HuaweiApiAvailability;
import com.huawei.hms.support.log.HMSLog;
import com.huawei.hms.update.manager.UpdateManager;
import com.huawei.hms.update.ui.UpdateBean;
import com.huawei.hms.utils.PackageManagerHelper;
import com.huawei.hms.utils.PackageManagerHelper.PackageStates;
import com.huawei.hms.utils.ResourceLoaderUtil;
import com.huawei.hms.utils.a;
import java.lang.ref.WeakReference;

public class b implements IBridgeActivityDelegate {
    private WeakReference<Activity> a;
    private int b;

    public void onBridgeActivityCreate(Activity activity) {
        this.a = new WeakReference(activity);
        Intent intent = activity.getIntent();
        if (intent == null) {
            c();
            return;
        }
        try {
            intent.getStringExtra("TestIntentProtocol");
            this.b = intent.getIntExtra(MapKey.UPDATE_VERSION, 0);
            if (this.b == 0) {
                c();
                return;
            }
            UpdateBean updateBean = new UpdateBean();
            updateBean.setHmsOrApkUpgrade(true);
            updateBean.setClientPackageName(a.a(activity.getApplicationContext()).b());
            updateBean.setClientVersionCode(this.b);
            updateBean.setClientAppId(HuaweiApiAvailability.APPID_HMS);
            if (ResourceLoaderUtil.getmContext() == null) {
                ResourceLoaderUtil.setmContext(activity.getApplicationContext());
            }
            updateBean.setClientAppName(ResourceLoaderUtil.getString("hms_update_title"));
            UpdateManager.startUpdate(activity, 1001, updateBean);
        } catch (Exception e) {
            HMSLog.e("UpdateAdapter", "intent has some error" + e.getMessage());
            c();
        }
    }

    public void onBridgeActivityDestroy() {
        HMSLog.i("UpdateAdapter", "onBridgeActivityDestroy");
        this.a = null;
    }

    public boolean onBridgeActivityResult(int i, int i2, Intent intent) {
        if (i != getRequestCode()) {
            return false;
        }
        HMSLog.i("UpdateAdapter", "onBridgeActivityResult");
        if (i2 == -1) {
            int intExtra = intent.getIntExtra(BridgeActivity.EXTRA_RESULT, -1);
            if (intExtra == 0) {
                HMSLog.i("UpdateAdapter", "Error resolved successfully!");
                com.huawei.hms.adapter.a.a.a().a(0);
            } else if (intExtra == 13) {
                HMSLog.i("UpdateAdapter", "Resolve error process canceled by user!");
                com.huawei.hms.adapter.a.a.a().a(13);
            } else if (intExtra == 8) {
                HMSLog.i("UpdateAdapter", "Internal error occurred, recommended retry.");
                com.huawei.hms.adapter.a.a.a().a(8);
            } else {
                HMSLog.i("UpdateAdapter", "Other error codes.");
                com.huawei.hms.adapter.a.a.a().a(intExtra);
            }
        } else if (i2 == 0) {
            Context b = b();
            if (b == null) {
                return true;
            }
            if (a(b, a.a(b.getApplicationContext()).b(), this.b)) {
                com.huawei.hms.adapter.a.a.a().a(0);
            } else {
                com.huawei.hms.adapter.a.a.a().a(25);
            }
        }
        a();
        return true;
    }

    public void onBridgeConfigurationChanged() {
        HMSLog.i("UpdateAdapter", "onBridgeConfigurationChanged");
    }

    public int getRequestCode() {
        return 1001;
    }

    public void onKeyUp(int i, KeyEvent keyEvent) {
        HMSLog.i("UpdateAdapter", "On key up when resolve conn error");
    }

    private void a() {
        Activity b = b();
        if (b != null && !b.isFinishing()) {
            b.finish();
        }
    }

    private Activity b() {
        if (this.a == null) {
            return null;
        }
        return (Activity) this.a.get();
    }

    private boolean a(Context context, String str, int i) {
        if (context == null || TextUtils.isEmpty(str) || i == 0) {
            return false;
        }
        PackageManagerHelper packageManagerHelper = new PackageManagerHelper(context);
        if (PackageStates.NOT_INSTALLED.equals(packageManagerHelper.getPackageStates(str)) || packageManagerHelper.getPackageVersionCode(str) < i) {
            return true;
        }
        return false;
    }

    private void c() {
        com.huawei.hms.adapter.a.a.a().a(8);
        a();
    }
}
